<template>
  <el-form label-width="80px">
    <h1>{{ id ? "编辑" : "新建" }}文章</h1>
    <el-form-item label="文章标题">
      <el-input v-model="list.title"></el-input>
    </el-form-item>

    <el-form-item label="文章内容">
      <vue-editor v-model="list.content"></vue-editor>
      <!-- <el-input v-model="list.content" type="textarea"></el-input> -->
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="save()">保存</el-button>
      <el-button>取消</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { VueEditor } from "vue2-editor";
export default {
  components: {
    VueEditor
  },
  props: {
    id: {},
  },
  data() {
    return {
      list: {},
    };
  },
  methods: {
    async fetch() {
      const res = await this.$http.get(`/detail/?id=${this.id}`);
      console.log('编辑')
      console.log(res)
      this.list = res.data.data[0];
    },
    async save() {
      if (this.id) {
        this.$router.push("/articles/index");
        const res = await this.$http.post("/article/edit/", this.list);
        console.log(res.data[0]);
      } else {
        this.$router.push("/articles/index");
        const res = await this.$http.post("article/create/", this.list);
        console.log(res);
      }
      // this.list = res.data;
    },
  },
  created() {
    this.id && this.fetch();
  },
};
</script>